import * as vscode from 'vscode';
import { ViewProvider } from './ViewProvider';

export class ViewManager {
    private readonly _context: vscode.ExtensionContext;

    constructor(constext: vscode.ExtensionContext) {
        this._context = constext;
    }

    private registerViewProvider(): vscode.Disposable {
        const provider = new ViewProvider(this._context);
        const disposable = vscode.window.registerWebviewViewProvider(
            'cheatsheet',
            provider
        );
        
        return disposable;
    }

    private registerPreviewMarkdown(): vscode.Disposable {
        const command = vscode.commands.registerCommand(
            'develop-mate.previewMarkdown',
            () => {
                const uri = vscode.window.activeTextEditor?.document.uri;
                if (uri && uri.fsPath.endsWith('.md')) {
                    vscode.commands.executeCommand('markdown.showPreviewToSide', uri);
                }
            }
        );
        return command;
    }

    subscribeDispoables() {
        const disposables = [
            this.registerViewProvider(),
            this.registerPreviewMarkdown()
        ];
        this._context.subscriptions.push(...disposables);
    }
}